This program will create a secret code for a spy
Contents
The spy's secret message is FIVE OCLOCK
The letters of the message are represented by numbers of the alphabet F I V E O C L O C K 6 9 22 5 15 3 12 15 3 11 The letters are arranged in pairs in a matrix array F V O L C I E C O K The associated numbers are put into a matrix M
M = [6 22 15 12 3; 9 5 3 15 11]
M =
6 22 15 12 3
9 5 3 15 11
A secret 2 x 2 matrix is used to encrypt the message
S=[5 4;7 11]
S =
5 4
7 11
The message is encrypted and stored in matrix C
C=S*M
C =
66 130 87 120 59
141 209 138 249 142
Modulo 26 is found to keep within 26 letters of alphabet
D=mod(C,26) % The message is now written in the corresponding letters of the aplhapbet % FI VE OC LO CK % NL ZA IH PO GL
D =
14 0 9 16 7
11 1 8 15 12
The message is decrypted with matrix I, the modular inverse of matrix S
I=[11 22;19 5];
E=I*D
% Modulo 26 is applied to keep within the number of letters in alphabet
Message=mod(E,26)
E =
396 22 275 506 341
321 5 211 379 193
Message =
6 22 15 12 3
9 5 3 15 11
A longer message is encrypted now
MEET YOU UNDER THE BRIDGE AT MIDNIGHT BRING MONEY AND PASSPORT M Y N T R E I G R M Y P P E O D H I A D H I O A A O E U E E D T N T N N N S R T U R B G M I B G E D S T A matrix is now formed using numbers to represent letters of alphabet
M=[13 25 14 20 18 5 9 7 18 13 25 16 16; 5 15 4 8 9 1 4 8 9 15 1 1 15;... 5 21 5 5 4 20 14 20 14 14 14 19 18; 20 21 18 2 7 13 9 2 7 5 4 19 20] % The message is encrypted using a 4 x 4 matrix S % S contains integers, non-singular,determinant is +ve & has inverse Mod 26 S=[5 2 4 3; 1 2 4 4; 5 1 7 3; 3 2 7 5] % The message is encrypted and stored in matrix C C=S*M % The modulo 26 of matrix C is found D=mod(C,26) % The numbers in matrix D are written as letters of the alphabet % YSIB POLS VJGS LLSQ OBRE PIWN FERV GGGE CPJW JOKJ MUBM GNKR HPUR 'YSIB POLS VJGS LLSQ OBRE PIWN FERV GGGE CPJW JOKJ MUBM GNKR HPUR' % Modular inverse of matrix S is found to decrypt message % First of all find the determinant of S F=det(S) % Use modular inverse of determinant mod 26 23 % Now find modular inverse of matrix S T=3*9*S^-1 % Now decrypt the message U=T*D V=mod(U,26) 'Since V=M the message has been shown to have been decrpyted'
M =
13 25 14 20 18 5 9 7 18 13 25 16 16
5 15 4 8 9 1 4 8 9 15 1 1 15
5 21 5 5 4 20 14 20 14 14 14 19 18
20 21 18 2 7 13 9 2 7 5 4 19 20
S =
5 2 4 3
1 2 4 4
5 1 7 3
3 2 7 5
C =
155 302 152 142 145 146 136 137 185 166 195 215 242
123 223 114 64 80 139 109 111 120 119 99 170 198
165 350 163 149 148 205 174 189 218 193 236 271 281
184 357 175 121 135 222 178 187 205 192 195 278 304
D =
25 16 22 12 15 16 6 7 3 10 13 7 8
19 15 10 12 2 9 5 7 16 15 21 14 16
9 12 7 19 18 23 18 7 10 11 2 11 21
2 19 19 17 5 14 22 5 23 10 13 18 18
ans =
YSIB POLS VJGS LLSQ OBRE PIWN FERV GGGE CPJW JOKJ MUBM GNKR HPUR
F =
9.0000
ans =
23
T =
12.0000 -33.0000 -18.0000 30.0000
-18.0000 144.0000 81.0000 -153.0000
-15.0000 48.0000 36.0000 -51.0000
21.0000 -105.0000 -72.0000 120.0000
U =
1.0e+03 *
Columns 1 through 7
-0.4290 0.0510 0.3780 -0.0840 -0.0600 -0.0990 0.2430
2.7090 -0.0630 -1.2960 0.4500 0.7110 0.7290 -1.2960
0.7590 -0.0570 -0.5670 0.2130 0.2640 0.3060 -0.3240
-1.8780 0.1770 1.1880 -0.3360 -0.5910 -0.5850 0.9450
Columns 8 through 13
-0.1230 0.0180 -0.2730 -0.1830 -0.0360 -0.2700
0.6840 -0.4590 1.3410 0.9630 0.0270 1.1070
0.2280 -0.0900 0.4560 0.2220 0.0450 0.4860
-0.4920 0.4230 -0.9570 -0.5160 0.0450 -0.8640
V =
Columns 1 through 7
13.0000 25.0000 14.0000 20.0000 18.0000 5.0000 9.0000
5.0000 15.0000 4.0000 8.0000 9.0000 1.0000 4.0000
5.0000 21.0000 5.0000 5.0000 4.0000 20.0000 14.0000
20.0000 21.0000 18.0000 2.0000 7.0000 13.0000 9.0000
Columns 8 through 13
7.0000 18.0000 13.0000 25.0000 16.0000 16.0000
8.0000 9.0000 15.0000 1.0000 1.0000 15.0000
20.0000 14.0000 14.0000 14.0000 19.0000 18.0000
2.0000 7.0000 5.0000 4.0000 19.0000 20.0000
ans =
Since V=M the message has been shown to have been decrpyted